﻿
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using DevTeam;


using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

//TODO K.A comboboxedit'e copy-paste yapılırsa izin verilenlerin haricinde değerler girilebiliyor!

namespace ConfExpert.Forms.Depo.Aksesuar
{
    public partial class AksesuarSeri : XPopupForm
    {
        int AksSeriId;
        public AksesuarSeri(int aksSeriId)
        {
            InitializeComponent();
            AksSeriId = aksSeriId;
            this.Text += "- " + AksSeriId.ToString();
        }

        DataTable dtDag;
        BindingSource bs;
        private void AksesuarSeri_Load(object sender, EventArgs e)
        {

            dtDag = ExecuteDataTable("GetDagilimVertical");

            bs = gvAna.Init("GetAksSeri", AksSeriId);
            gvAna.AddDetailGrid(gvDetay, "DagilimId");

            //bs.Filter = "DagilimId>0";

            gvDetay.MakeColumnInvisible("DagilimId","AksSeriId","AksSeriLinesId");
            gvDetay.MakeReadOnly("DBoy");
            gvDetay.Columns["DBoy"].ColumnEdit = riDag;
            gvAna.MakeReadOnly();
            gleTumu.Edit.Assign(RI(Rp.Dagilim));
            gleTumu.EditValueChanged += GleTumu_EditValueChanged;
            btnKaydet.ItemClick += btnKaydet_ItemClick;

            gvAna.AddSummary();
            gvDetay.AddSummary();

            riDag.TextEditStyle = TextEditStyles.DisableTextEditor;
            bs.CurrentChanged += bs_CurrentChanged;
            bs_CurrentChanged(null, null);

            gvDetay.RowStyle += gvDetay_RowStyle;

            gvAna.BestFitColumns();
            gvDetay.BestFitColumns();

            GridManager gm = GridManager.GetManager(gvAna);
            gm.GridMenu.AddMenu("Bu DagilimId'ye bağlı kayıtları sil", DagilimIdSil);

        }

        private void GleTumu_EditValueChanged(object sender, EventArgs e)
        {
            gvAna.RefreshDataSource("AddAksSeri", AksSeriId, intParse(gleTumu.EditValue));
            gvAna.FocusedRowHandle = gvAna.LocateByValue("DagilimId", intParse(gleTumu.EditValue));

            gvAna.BestFitColumns();
            gvDetay.BestFitColumns();
        }

        private void DagilimIdSil(object sender, EventArgs e)
        {
            gvAna.RefreshDataSource(true, "DelAksSeri", AksSeriId, intParse(bs, "DagilimId"));
        }

        void gvDetay_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            GridView view = sender as GridView;
            if (e.RowHandle > -1)
            {
                string boy = view.GetRowCellValue(e.RowHandle, "Boy").ToString();
                string dboy = view.GetRowCellValue(e.RowHandle, "DBoy").ToString();

                if (boy == dboy)
                    e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
            }
        }

        void bs_CurrentChanged(object sender, EventArgs e)
        {
            riDag.Items.Clear();
            if (strParse(bs, "DagilimId") != string.Empty)
                foreach (DataRow dr in dtDag.Select("DagilimId=" + strParse(bs, "DagilimId")))
                    riDag.Items.Add(dr["Beden"].ToString());
        }


        RepositoryItemComboBox riDag = new RepositoryItemComboBox();

        void btnKaydet_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            gvAna.EndEditing();
            gvDetay.EndEditing();
            DataTable dt = ExecuteDataTable("UpdAksSeri", 1, AksSeriId, gvAna.Dataset().Tables[1].Serialize());
            if (dt.Rows.Count > 0)
                //    XtraMessageBox.Show("Başarıyla Güncellendi");
                //else
                XtraMessageBox.Show(dt.Rows[0][0].ToString(), "Dikkat!");

            //gvDetay.Update("DBoy", "AksSeriLines");
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
